import { configureStore } from '@reduxjs/toolkit';
import { TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux';

// 导入所有的slice
import authSlice from './slices/authSlice';
import userSlice from './slices/userSlice';
import shopSlice from './slices/shopSlice';
import productSlice from './slices/productSlice';
import orderSlice from './slices/orderSlice';
import chatSlice from './slices/chatSlice';
import salesSlice from './slices/salesSlice';
import uiSlice from './slices/uiSlice';

// 配置store
export const store = configureStore({
  reducer: {
    auth: authSlice,
    user: userSlice,
    shop: shopSlice,
    product: productSlice,
    order: orderSlice,
    chat: chatSlice,
    sales: salesSlice,
    ui: uiSlice,
  },
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: {
        // 忽略这些action types的序列化检查
        ignoredActions: [
          'persist/PERSIST',
          'persist/REHYDRATE'
        ],
        // 忽略这些路径的序列化检查
        ignoredPaths: [
          'register'
        ],
        // 忽略这些action字段的序列化检查
        ignoredActionsPaths: ['meta.arg', 'payload.timestamp'],
      },
    }),
  devTools: import.meta.env.NODE_ENV !== 'production',
});

// 导出类型
export type RootState = ReturnType<typeof store.getState>;
export type AppDispatch = typeof store.dispatch;

// 导出类型化的hooks
export const useAppDispatch = () => useDispatch<AppDispatch>();
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;

export default store;
